import { requestClient } from '#/api/request';

export namespace MenuApi {
  /** 菜单信息 */
  export interface Menu {
    id?: number;
    systemId: number;
    pid: number;
    sort: number;
    menuType: 'C' | 'F' | 'M'; // M目录 C菜单 F按钮
    path: string;
    component: string;
    permission: string;
    name: string;
    title: string;
    icon: string;
    hideInMenu: number; // 0不隐藏 1隐藏
    ignoreKeepAlive: number; // 0不取消 1取消
    linkFlag: number; // 0不是外链 1是外链
    link: string;
    disabled: number; // 0不停用 1停用
    handler: number;
    handlerName: string;
    createTime: string;
    updateTime: string;
    apiIds?: number[]; // 关联的API ID数组
    children?: Menu[]; // 子菜单（用于树形结构）
  }
}

/** 查询菜单列表 */
export function getMenuList(systemIdOrParams?: number | { systemId: number }) {
  let systemId: number | undefined;

  if (typeof systemIdOrParams === 'number') {
    systemId = systemIdOrParams;
  } else if (systemIdOrParams && typeof systemIdOrParams === 'object') {
    systemId = systemIdOrParams.systemId;
  }

  return requestClient.get<MenuApi.Menu[]>(`/menu/list?systemId=${systemId}`);
}

/** 查询菜单详情 */
export function getMenu(id: number) {
  return requestClient.get<MenuApi.Menu>(`/menu/info?id=${id}`);
}

/** 新增菜单 */
export function createMenu(data: MenuApi.Menu) {
  return requestClient.post('/menu/create', data);
}

/** 修改菜单 */
export function updateMenu(data: MenuApi.Menu) {
  return requestClient.put('/menu/update', data);
}

/** 删除菜单 */
export function deleteMenu(id: number) {
  return requestClient.delete(`/menu/delete?id=${id}`);
}
